package io.renren.modules.generator.controller;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.MissUserEntity;
import io.renren.modules.generator.service.MissUserService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-05-16 23:22:15
 */
@RestController
@RequestMapping("generator/missuser")
public class MissUserController {
    @Autowired
    private MissUserService missUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = missUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Integer userId){
		MissUserEntity missUser = missUserService.getById(userId);

        return R.ok().put("missUser", missUser);
    }

    /**
     * 停用
     */
    @RequestMapping("/ban/{userId}")
    public R ban(@PathVariable("userId") Integer userId){
        missUserService.banUser(userId);

        return R.ok();
    }

    /**
     * 启用
     */
    @RequestMapping("/start/{userId}")
    public R start(@PathVariable("userId") Integer userId){
        missUserService.startUser(userId);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MissUserEntity missUser){
        String pass = DigestUtils.md5DigestAsHex(missUser.getPassword().getBytes(StandardCharsets.UTF_8));
        missUser.setPassword(pass);
        missUserService.save(missUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MissUserEntity missUser){
        String pass = DigestUtils.md5DigestAsHex(missUser.getPassword().getBytes(StandardCharsets.UTF_8));
        missUser.setPassword(pass);
		missUserService.updateById(missUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userIds){
		missUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
